#
# ClassicLadder project - Makefile
# Copyright (C) 2001-2012 Marc Le Douarain
# http://membres.lycos.fr/mavati/classicladder/
# http://www.sourceforge.net/projects/classicladder
#
# Last update : June 2012
#

# Comment if you do not want the GTK interface (for an embedded version)
GTK_INTERFACE = 1

# Compile support for Xenomai (real-time extension for Linux, see www.xenomai.org)
#XENOMAI_SUPPORT = 1

# Compile for Windows
###WINDOWS = 1

# Uncomment if you want to debug a 'segmentation fault' with gdb or KDbg for example... ;-)
###DEBUG = 1

# To add extra features like events log, monitor protocol (perhaps not required when only rung/grafcet motor engine used)
COMPLETE_PLC = 1

# Uncomment if you want to build a static library
# BUILD_LIB = 1

#Now on Windows using library "inpout32.dll" !
###ifndef WINDOWS
# Comment if you do not want the I/O access (not x86 platform)
# Used in the hardware.c file.
MAKE_IO_ACCESS = 1
###endif

# Uncomment the following flag to make the MAT connection version
####MAT_CONNECTION = 1

# Uncomment if you want to be able to use comedi drivers (see www.comedi.org)
###export COMEDI_SUPPORT = 1

# Comment if you do not want to support this language
export SEQUENTIAL_SUPPORT = 1
# Comment if you do not want to have the old timers/monostables blocks (usefull for compatibility with old projects before IEC timers)
OLD_TIMERS_MONOS = 1
# for EMC usage, compatibility with old versions without this input
###FORCE_INP_CONTROL_OLD_TIMERS_FLAG = 1

# No more used... gtk-print now !
#ifdef GTK_INTERFACE
#ifndef WINDOWS
# Comment to disable gnome_print dependencies...
###GNOME_PRINT=1
#endif
#endif


# Cross-compilation possible here...
CROSS =
###CROSS = /usr/local/arm/4.5.1/bin/arm-linux-
CC = $(CROSS)gcc
AR = $(CROSS)ar
PREFIX=/usr
BINDIR=${PREFIX}/bin
LIBDIR=${PREFIX}/lib
INCLUDEDIR=${PREFIX}/include

# Embedded special flags...
ifndef GTK_INTERFACE
OWN_CFLAGS = -march=i486
OWN_LIBS = -march=i486
endif

ifdef GTK_INTERFACE
OWN_CFLAGS += -Wall `pkg-config gtk+-2.0 --cflags` `pkg-config pango --cflags` `pkg-config gthread-2.0 --cflags` -DGTK_INTERFACE -DGTK2
OWN_LIBS += `pkg-config gtk+-2.0 --cflags --libs` `pkg-config pango --cflags --libs` `pkg-config gthread-2.0 --cflags --libs`
###ifdef GNOME_PRINT
###OWN_CFLAGS += `pkg-config --cflags libgnomeprintui-2.2` -DGNOME_PRINT_USE
###OWN_LIBS += `pkg-config --libs libgnomeprintui-2.2`
###endif
endif

#Windows
ifdef WINDOWS
OWN_LIBS += -lwsock32 -lz
endif

OWN_CFLAGS += -DMODBUS_IO_MASTER

# the pthread is included with gthread2.0 of pkg-config
ifndef GTK_INTERFACE
OWN_LIBS += -lpthread
endif

ifdef SEQUENTIAL_SUPPORT
OWN_CFLAGS += -DSEQUENTIAL_SUPPORT
endif
ifdef OLD_TIMERS_MONOS
OWN_CFLAGS += -DOLD_TIMERS_MONOS_SUPPORT
endif
ifdef FORCE_INP_CONTROL_OLD_TIMERS_FLAG
OWN_CFLAGS += -DFORCE_INP_CONTROL_OLD_TIMERS
endif
ifdef COMPLETE_PLC
OWN_CFLAGS += -DCOMPLETE_PLC
ifndef GTK_INTERFACE
#OWN_LIBS += -lm
OWN_LIBS += -lz
endif
endif

ifdef COMEDI_SUPPORT
OWN_CFLAGS += -DCOMEDI_SUPPORT
COMEDI_LIB = -lcomedi
endif
ifdef MAKE_IO_ACCESS
OWN_CFLAGS += -DMAKE_IO_ACCESS
endif

ifdef XENOMAI_SUPPORT
OWN_CFLAGS += `xeno-config --skin posix --cflags`
OWN_LIBS += `xeno-config --skin posix --ldflags`
endif

ifdef DEBUG
OWN_CFLAGS += -g -DNO_SEGV_TRAP
endif

RM = rm

OBJ_COMMON = calc_.o files.o files_project.o arithm_eval_.o manager_.o arrays_.o socket_server.o protocol_modbus_slave_.o symbols.o vars_system.o tasks.o
ifdef COMPLETE_PLC
OBJ_COMMON += log_events.o monitor_protocol.o monitor_transfer.o base64.o monitor_protocol_adds_serial.o monitor_threads.o monitor_sockets_udp.o cJSON.o preferences.o
endif
OBJ_COMMON += socket_modbus_master.o protocol_modbus_master.o
ifdef GTK_INTERFACE
OBJ_GTK_INTERFACE = drawing.o edit.o edit_gtk.o editproperties_gtk.o manager_gtk.o classicladder_gtk.o config_gtk.o vars_names.o symbols_gtk.o spy_vars_gtk.o print_gtk.o edit_copy.o menu_and_toolbar_gtk.o monitor_windows_gtk.o search.o
ifdef COMPLETE_PLC
OBJ_GTK_INTERFACE += log_events_gtk.o 
endif
OBJ_CLASSICLADDER = $(OBJ_COMMON) $(OBJ_GTK_INTERFACE) vars_access_.o
else
OBJ_CLASSICLADDER = $(OBJ_COMMON) vars_access_.o
endif
ifdef WINDOWS
OBJ_CLASSICLADDER += serial_win.o
else
OBJ_CLASSICLADDER += serial_linux.o
endif

###ifdef GNOME_PRINT
###OBJ_CLASSICLADDER += print_gnome.o
###endif

OBJ_CLASSICLADDER_LIB = $(OBJ_COMMON) ladderlib.o

ifdef SEQUENTIAL_SUPPORT
OBJ_CLASSICLADDER_LIB += files_sequential.o calc_sequential_.o
OBJ_CLASSICLADDER += files_sequential.o calc_sequential_.o
ifdef GTK_INTERFACE
OBJ_CLASSICLADDER += drawing_sequential.o edit_sequential.o
endif
endif

# Some objects names end with _ because there are not identical if compiled for RTLinux or not.

ifdef MAT_CONNECTION
MAT = ../..
OBJ_CLASSICLADDER_MAT = $(OBJ_COMMON) vars_mat.o $(MAT)/lib/libmatplc.la -lrt
all: classicladder classicladder-mat
else
ifdef BUILD_LIB
all: classicladder-lib
else
all: classicladder
endif
endif

classicladder: $(OBJ_CLASSICLADDER) classicladder_.o hardware_.o
	$(CC) -o classicladder $(OBJ_CLASSICLADDER) hardware_.o classicladder_.o $(OWN_LIBS) $(COMEDI_LIB)

classicladder-mat: $(OBJ_CLASSICLADDER_MAT) classicladder_mat.o
	libtool $(CC) -o classicladder-mat $(OBJ_CLASSICLADDER_MAT) classicladder_mat.o $(OWN_LIBS)

classicladder-lib: libladder.a

libladder.a: $(OBJ_CLASSICLADDER_LIB)
	$(AR) -r libladder.a $(OBJ_CLASSICLADDER_LIB)

rtl_support: OWN_CFLAGS+=-DRT_SUPPORT -DRTLINUX
rtl_support: $(OBJ_CLASSICLADDER) classicladder_rtl_support.o
	$(CC) -o classicladder_rtl_support $(OBJ_CLASSICLADDER) classicladder_rtl_support.o $(OWN_LIBS)
	make -f Makefile_rtlinux

rtai_support: OWN_CFLAGS+=-DRT_SUPPORT -DRTAI -I/usr/src/rtai/include
rtai_support: $(OBJ_CLASSICLADDER) classicladder_rtai_support.o
	$(CC) -o classicladder_rtl_support $(OBJ_CLASSICLADDER) classicladder_rtai_support.o $(OWN_LIBS)
	make -f Makefile_rtai
	make test2 -f Makefile_rtai

test:
	make test -f Makefile_rtlinux


classicladder_.o : classicladder.c classicladder.h global.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)
classicladder_rtl_support.o : classicladder.c classicladder.h global.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS) -DRT_SUPPORT -DRTLINUX
classicladder_rtai_support.o : classicladder.c classicladder.h global.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS) -DRT_SUPPORT -DRTAI
classicladder_mat.o : classicladder.c classicladder.h global.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS) -DMAT_CONNECTION

classicladder_gtk.o : classicladder_gtk.c classicladder.h global.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

calc_.o : calc.c classicladder.h global.h calc.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

###config.o : config.c classicladder.h global.h config.h
###	$(CC) -c $< -o $@ $(OWN_CFLAGS)

arrays_.o : arrays.c classicladder.h global.h calc.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

drawing.o : drawing.c classicladder.h global.h drawing.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

drawing_sequential.o : drawing_sequential.c classicladder.h global.h drawing_sequential.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

edit.o : edit.c classicladder.h global.h edit.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

edit_gtk.o : edit_gtk.c classicladder.h global.h edit_gtk.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

editproperties_gtk.o : editproperties_gtk.c classicladder.h global.h editproperties_gtk.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

manager_gtk.o : manager_gtk.c classicladder.h global.h manager_gtk.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

config_gtk.o : config_gtk.c classicladder.h global.h config_gtk.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

files.o : files.c classicladder.h global.h files.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

files_project.o : files_project.c classicladder.h global.h files.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

files_sequential.o : files_sequential.c classicladder.h global.h files_sequential.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

ladderlib.o : ladderlib.c ladderlib.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

ladderlib.h: classicladder.h global.h ladderlib.head ladderlib.tail
	cat ladderlib.head classicladder.h global.h ladderlib.tail >ladderlib.h

vars_access_.o : vars_access.c classicladder.h global.h vars_access.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

arithm_eval_.o : arithm_eval.c classicladder.h global.h vars_access.h arithm_eval.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

manager_.o : manager.c classicladder.h global.h vars_access.h manager.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

vars_mat.o : vars_mat.c
	$(CC) -c $< -o $@ $(OWN_CFLAGS) -DMAT_CONNECTION

calc_sequential_.o : calc_sequential.c classicladder.h global.h calc_sequential.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

hardware_.o : hardware.c classicladder.h global.h hardware.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)
#	$(CC) -c $< -o $@ $(OWN_CFLAGS) -I../comedi/comedilib-0.7.20/include/

edit_sequential.o : edit_sequential.c classicladder.h global.h edit_sequential.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

socket_server.o : socket_server.c classicladder.h global.h socket_server.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

protocol_modbus_slave_.o : protocol_modbus_slave.c classicladder.h global.h protocol_modbus_slave.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

socket_modbus_master.o : socket_modbus_master.c classicladder.h global.h socket_modbus_master.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

protocol_modbus_master.o : protocol_modbus_master.c classicladder.h global.h protocol_modbus_master.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

serial_linux.o : serial_linux.c serial_common.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)
serial_win.o : serial_win.c serial_common.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

###print_gnome.o : print_gnome.c classicladder.h global.h print_gnome.h
###	$(CC) -c $< -o $@ $(OWN_CFLAGS)

symbols_gtk.o : symbols_gtk.c classicladder.h global.h symbols_gtk.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

symbols.o : symbols.c classicladder.h global.h symbols.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

spy_vars_gtk.o : spy_vars_gtk.c classicladder.h global.h spy_vars_gtk.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

vars_names.o : vars_names.c classicladder.h global.h vars_names.h vars_names_list.c
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

print_gtk.o : print_gtk.c classicladder.h global.h print_gtk.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

edit_copy.o : edit_copy.c classicladder.h global.h edit_copy.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

vars_system.o : vars_system.c classicladder.h global.h vars_system.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

log_events.o : log_events.c classicladder.h global.h log_events.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

log_events_gtk.o : log_events_gtk.c classicladder.h global.h log_events_gtk.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

menu_and_toolbar_gtk.o : menu_and_toolbar_gtk.c classicladder.h global.h menu_and_toolbar_gtk.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

cJSON.o : cJSON.c cJSON.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)
monitor_protocol.o : monitor_protocol.c classicladder.h global.h monitor_protocol.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)
monitor_threads.o : monitor_threads.c classicladder.h global.h monitor_threads.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)
monitor_sockets_udp.o : monitor_sockets_udp.c classicladder.h global.h monitor_sockets_udp.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)
monitor_protocol_adds_serial.o : monitor_protocol_adds_serial.c classicladder.h global.h monitor_protocol_adds_serial.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)
monitor_windows_gtk.o : monitor_windows_gtk.c classicladder.h global.h monitor_windows_gtk.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)
monitor_transfer.o : monitor_transfer.c classicladder.h global.h monitor_transfer.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)
base64.o : base64.c base64.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

preferences.o : preferences.c preferences.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

search.o : search.c search.h classicladder.h global.h 
	$(CC) -c $< -o $@ $(OWN_CFLAGS)

tasks.o : tasks.c tasks.h
	$(CC) -c $< -o $@ $(OWN_CFLAGS)


PHONY: all install clean dist

install: all
	install -d ${DESTDIR}${BINDIR}
	install -d ${DESTDIR}${INCLUDEDIR}
	install -d ${DESTDIR}${LIBDIR}
	install classicladder ${DESTDIR}${BINDIR}
#	install ladderlib.h ${DESTDIR}${INCLUDEDIR}
#	install libladder.a ${DESTDIR}${LIBDIR}


clean:
	$(RM) -f *.o
	$(RM) -f classicladder_rtl_support
	$(RM) -f classicladder-mat
#	$(RM) -f classicladder
	$(RM) -f ladderlib.h
	$(RM) -f libladder.a
	$(RM) -f classicladder.exe

#TODO: get name of the current directory to zip !
dist: clean
	(cd ..;rm -f classicladder.zip;zip -r classicladder.zip classicladder/*)

dist_embedded:
	rm -f classicladder_embedded.tar.gz
	tar -cf classicladder.tar classicladder classicladder_run.script
	gzip classicladder.tar
